package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.CategoryItemDao;
import com.ktjy.ktmall.entity.CategoryItem;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @auth wzy
 * @date 2024/10/12 10:51
 */
public class CategoryItemDaoImpl extends BaseDao implements CategoryItemDao {
    @Override
    public List<CategoryItem> getOneLevelCategory() {
        List<CategoryItem> list = new ArrayList<>();
        String sql = "select * from t_goods_category where category_level = 1 and `enabled` = 0 limit 10";
        rs = super.executeQuery(sql);
        try {
            while (rs.next()) {
                // 只需要读取到分类 id、分类名称、分类图标
                CategoryItem c = new CategoryItem(rs.getInt(1), rs.getInt(2),
                        rs.getInt(3), rs.getString(4), rs.getInt(5),
                        rs.getString(6));

                list.add(c);

            }
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            super.closeAll(rs, pstmt, conn);

        }
        return list;

    }

    @Override
    public List<CategoryItem> findAllCategoryList() {
        List<CategoryItem> list = new ArrayList<>();
        String sql = "select * from t_goods_category where `enabled` = 0";
        rs = super.executeQuery(sql);
        try {
            while (rs.next()) {
                // 只需要读取到分类 id、分类名称、分类图标
                CategoryItem c = new CategoryItem(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getInt(5), rs.getString(6));
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public CategoryItem findById(int id) {
        CategoryItem c = null;
        String sql = "select * from t_goods_category where `enabled` = 0 AND `id` =?";
        rs = super.executeQuery(sql, id);
        try {
            while (rs.next()) {
                // 只需要读取到分类 id、分类名称、分类图标
                c = new CategoryItem(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getInt(5), rs.getString(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return c;
    }
}
